new Vue({
    el: '#app',
    data: {
        SERVER_HOST: SERVER_HOST,
        prizeId: getUrlKey('prizeId'),
        prize: {},
        member: {},
        prizeName: '',
        prizeUrl: ''
    },
    mounted() {
        var member = Cookies.get('member');
        if (!member || member == null || member == '') {
            window.location.href = 'dl.html';
        } else {
            this.member = JSON.parse(member);
        }
        if (this.prizeId != null && this.prizeId != '') {
            getAjax().post('/web/getPrize', toFormData({
                prizeId: this.prizeId
            })).then(res => {
                if (res.data.data) {
                    let prize = res.data.data;
                    this.prize = res.data.data;
                    this.prize.createtime = getDate(this.prize.createtime);
                    this.prizeUrl = this.SERVER_HOST + '/' + prize.imgpath;
                    let themeNmae = prize.theme ? prize.theme.name : '';
                    let dipName = prize.diplomas ? ' ' + prize.diplomas.name : '';
                    this.prizeName = themeNmae + dipName;
                }
            });
        }
    },
    methods: {
        exit() {
            Cookies.remove('member');
            window.location.href = 'dl.html';
        },
        prizeDownload (id) {
            getAjax().post('/busine/prizes/prizeDownload', toFormData({
                id: id
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    alert('下载完成！');
                }
            });
        },
        prizeDownloadPDF (id, url, name) {
            getAjax().post('/busine/prizes/prizeDownload', toFormData({
                id: id
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    let Img = new Image();
                    let dataURL = '';
                    Img.setAttribute('crossOrigin', 'anonymous');
                    Img.src = url;
                    Img.onload = function () { // 要先确保图片完整获取到，这是个异步事件
                        let canvas = document.createElement('canvas'); // 创建canvas元素
                        canvas.width = Img.width;
                        canvas.height = Img.height;
                        canvas.getContext('2d').drawImage(Img, 0, 0, Img.width, Img.height); // 将图片绘制到canvas中
                        dataURL = canvas.toDataURL('image/jpeg'); // 转换图片为dataURL
                        let width = '';
                        let height = '';
                        let aspect = '';
                        let widthMargin = 0;
                        let heightMargin = 0;
                        let widthScale = Img.width / 595.28;
                        let heightScale = Img.height / 841.89;
                        if (widthScale > heightScale) {
                            aspect = 'landscape';
                            width = 841.89;
                            height = 841.89 / Img.width * Img.height;
                            heightMargin = (595.28 - height) / 2;
                        } else {
                            height = 841.89;
                            width = 841.89 / Img.height * Img.width;
                            widthMargin = (595.28 - width) / 2;
                        }
                        var doc = new jsPDF(aspect, 'pt', 'a4');
                        doc.addImage(dataURL, 'PNG', widthMargin, heightMargin, width, height);
                        doc.save(name + '.pdf');
                        alert('下载完成！');
                    };
                }
            });
        }
    }
});
